const fs = require('fs');
const path = require('path');

const rootDir =  process.cwd();

const getGitCommit = () => {
  try {
    let _gv = fs.readFileSync(path.resolve(rootDir, '.git/HEAD')).toString().trim();
    if (_gv.indexOf(':') === -1) return _gv;
    let _gc = fs.readFileSync(path.resolve(rootDir, '.git', _gv.substring(5))).toString().trim();
    _gc = _gc.length > 7 ? _gc.slice(0, 7) : _gc
    return _gc
  } catch (err) {
    throw Error('===ERROR: GIT未初始化或未获取到Git提交信息===');
  }
};

module.exports = getGitCommit;
